import {
	BASE_URL
} from "@/env.js";

const install = (Vue) => {
	Vue.prototype.$u.http.setConfig({
		baseUrl: BASE_URL,
		dataType: 'json',
		header: {
			'content-type': 'application/json'
		},
	});
	Vue.prototype.$u.http.interceptor.request = async (config) => {
		if (config.url == '/loginMemberWX') {
			config.header['content-type'] = 'application/x-www-form-urlencoded'
		}
		const token = uni.getStorageSync('TOKEN');
		config['header']['fsmtoken'] = token;
		config['header']['Cookie'] = `fsmToken=${token}`;
		config['header']['Tenantid'] = uni.getStorageSync('tenantid');
		return config;
	}

	Vue.prototype.$u.http.interceptor.response = (res) => {
		if (res.code == 200) {
			return res;
		} else if (res.code == 401 && /登录/.test(res.message)) {
			Vue.prototype.$u.toast(res.message);
		} else if (res.code == 403 && /权限/.test(res.message)) {
			Vue.prototype.$u.toast(res.message);
			setTimeout(() => {
				uni.reLaunch({
					url: '/pages/mine/index'
				})
			}, 800)
		} else if (res.code == 505 && /登录/.test(res.message)) {
			Vue.prototype.$u.toast('登录已失效，请重新登录');
			// uni.removeStorageSync('TOKEN');
			uni.reLaunch({
				url: '/pages/public/loginAgain'
			})
			return false;
		} else if (res.code == 500 && /密码/.test(res.message)) {
			Vue.prototype.$u.toast(res.message);
			return false;
		} else if (res.code == 400) {
			Vue.prototype.$u.toast(res.message);
			return false;
		} else {
			Vue.prototype.$u.toast(res.message);
			return false;
		}
	}
}

export default {
	install
}